package com.jic.member.controller.manage;

import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.member.constant.consist.ResponseCode;
import com.jic.member.entity.TLabelMerchantRelation;
import com.jic.member.request.TLabelMerchantRelationRequest;
import com.jic.member.request.TLabelRequest;
import com.jic.member.response.TLabelMerchantRelationResponse;
import com.jic.member.response.TLabelResponse;
import com.jic.member.service.TLabelMerchantRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: 建投数据科技(山东)有限公司
 * @date: 2020-06-07 13:35:51
 */
@RestController
@Slf4j
@RequestMapping("/tLabelMerchantRelation")
@Api(tags = " [开放给商家标签]控制类-PC")
public class TLabelMerchantRelationController {
    @Autowired
    private TLabelMerchantRelationService service;

    /**
     * 新增记录
     *
     * @param request 新增[开放给商家标签]请求参数
     * @return TLabelMerchantRelationResponse 新增[开放给商家标签]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "新增[开放给商家标签]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult save(@RequestBody TLabelMerchantRelationRequest request) throws Throwable {
        return RestResult.success(service.save(request));
    }

    /**
     * 新增或修改记录
     *
     * @param request 新增或修改[开放给商家标签]请求参数
     * @return TLabelMerchantRelationResponse 新增或修改[开放给商家标签]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "保存或更新[开放给商家标签]")
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public RestResult<Long> saveOrUpdate(@Validated @RequestBody TLabelMerchantRelationRequest request) throws Throwable {
        TLabelMerchantRelation tLabelMerchantRelation = new TLabelMerchantRelation();
        BeanUtils.copyProperties(request, tLabelMerchantRelation);
        long flag = service.saveOrUpdate(tLabelMerchantRelation);
        return RestResult.success(flag);
    }

    /**
     * 删除记录
     *
     * @param request 删除[开放给商家标签]请求参数
     * @return TLabelMerchantRelationResponse 删除[开放给商家标签]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "删除[开放给商家标签]记录")
    @PostMapping("/delete")
    @ResponseBody
    public RestResult<Long> delete(@RequestBody TLabelMerchantRelationRequest request) throws Throwable {
        TLabelMerchantRelation tLabelMerchantRelation = new TLabelMerchantRelation();
        BeanUtils.copyProperties(request, tLabelMerchantRelation);
        long flag = service.deleteByPrimaryKeyLogically(tLabelMerchantRelation);
        return RestResult.success(flag);
    }

    /**
     * 修改记录
     *
     * @param request 修改[开放给商家标签]请求参数
     * @return TLabelMerchantRelationResponse 修改[开放给商家标签]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "修改[开放给商家标签]记录")
    @PostMapping("/update")
    @ResponseBody
    public RestResult update(@RequestBody TLabelMerchantRelationRequest request) throws Throwable {
        return RestResult.success(service.updateByPrimaryKeySelective(request));
    }

    /**
     * 单表查询详情信息
     *
     * @param request 查询[开放给商家标签]详情请求参数
     * @return TLabelMerchantRelationResponse 查询[开放给商家标签]详情响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @PostMapping("/getByPrimaryKey")
    @ApiOperation(value = "查询[开放给商家标签]详情")
    @ResponseBody
    public RestResult<TLabelMerchantRelationResponse> getByPrimaryKey(@Validated @RequestBody TLabelMerchantRelationRequest request) {
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        if (request != null && request.getId() != null) {

            TLabelMerchantRelation tLabelMerchantRelation = new TLabelMerchantRelation();
            BeanUtils.copyProperties(request, tLabelMerchantRelation);
            TLabelMerchantRelation result = service.getByPrimaryKey(tLabelMerchantRelation);
            log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
            if (result == null) {
                return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
            }
            TLabelMerchantRelationResponse bo = new TLabelMerchantRelationResponse();
            BeanUtils.copyProperties(result, bo);
            return RestResult.success(bo);
        }
        log.info("查询详情请求结束");
        return RestResult.success(new TLabelMerchantRelationResponse());
    }


    /**
     * T_LABEL_MERCHANT_RELATION 查询列表（不分页）
     *
     * @param request 前端请求参数
     */
    @PostMapping("/listAll")
    @ApiOperation(value = "不分页查询 [开放给商家标签] ")
    @ResponseBody
    public RestResult<List<TLabelMerchantRelationResponse>> listTLabelMerchantRelationAllRecord(@RequestBody TLabelMerchantRelationRequest request) throws Throwable {
        TLabelMerchantRelation param = new TLabelMerchantRelation();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        List<TLabelMerchantRelation> list = service.listAllRecord(param);
        List<TLabelMerchantRelationResponse> responseList = list.stream().map(temp -> {
            TLabelMerchantRelationResponse bo = new TLabelMerchantRelationResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(responseList);
    }


    /**
     * 分页查询
     *
     * @param request 分页查询请求参数
     * @return 分页列表
     * @throws Throwable 业务异常和系统异常
     */
    @PostMapping("/listPage")
    @ApiOperation(value = " 分页查询列表 ")
    @ResponseBody
    public RestResult<PageResult<TLabelResponse>> listPage(@RequestBody TLabelRequest request) throws Throwable {
        log.info("开始进入商家标签分页查询列表=========》,{}", request.toString());
        Page page = new Page();
        page.setLimit(request.getLimit());
        page.setStart(request.getStart());
        return RestResult.success(service.listPageBySelective(request,page));
    }
}

